package com.sky.haikangdemo.api;

import com.sky.haikangdemo.config.PlateConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

/**
 * @Author: Yutiansong
 * @CreateTime: 2025/01/23 11:23
 * @Description: TODO 1
 * @Version: 1.0
 */
@RestController
public class TestController {

    @GetMapping("/device-info")
    public String getDeviceInfo() throws IOException {
        String url = "http://" + PlateConfig.deviceIp + ":" + PlateConfig.devicePort + "/ISAPI/System/deviceInfo";
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("GET");
        String auth = Base64.getEncoder().encodeToString((PlateConfig.username + ":" + PlateConfig.password).getBytes());
        connection.setRequestProperty("Authorization", "Basic " + auth);

//        int responseCode = connection.getResponseCode();
//        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            return response.toString();
//        } else {
//            throw new RuntimeException("Failed to get device info, response code: " + responseCode);
//        }
    }
}
